<?php

/**
 * @copyright   FOSP
 * @version     20120409
 * @todo        Mysql access to player_team_match table
 * @name        Fpt_Business_PlayerTeamMatch_Adapter_Mysql
 * @author      QuangTM 
 */
class Fpt_Business_PlayerTeamMatch_Adapter_Mysql extends Fpt_Business_PlayerTeamMatch_Adapter_Abstract
{
    /**
     * Get Fpt_Business_PlayerTeamMatch_Adapter_Mysql's instance
     * @return Fpt_Business_PlayerTeamMatch_Adapter_Mysql
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }
    
    /**
     * Get list player by team and match
     * @param int $matchID
     * @param int $teamID
     * @return array|boolean
     * @author QuangTM
     */
    public function getPlayersTeamMatch($matchID, $teamID)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_sport_getDetailPlayerTeamMatch(:p_teamid, :p_matchid);');

            // Bind param
            $stmt->bindParam('p_teamid', $teamID, PDO::PARAM_INT);
            $stmt->bindParam('p_matchid', $matchID, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = array ();

            // Get format data
            $formatInstance = new Fpt_Business_PlayerTeamMatch_Metadata_PlayerTeamMatch();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $result[$row['player_id']] = $formatInstance->init($row)->getFormatedData();
            }

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }
    
}